package com.misyi.designpattern.strategy;

/**
 * 策略模式
 *
 * @author licong
 * @date 2021-09-14 下午5:21
 */
public class StrategyPatternDemo {

    public static void main(String[] args) {
        int discountStyle = 2;
        DiscountStrategy strategy = DiscountStrategyFactory.getDiscountStrategy(discountStyle);
        strategy.execute();

        // 或者使用Context 调用
        Context context = new Context(strategy);
        context.execuete();
    }


    public interface DiscountStrategy {
        void execute();
    }

    public static class DiscountStrategyA implements DiscountStrategy {

        @Override
        public void execute() {
            System.out.println("执行优惠计价方式A 的复杂逻辑");
        }
    }

    public static class DiscountStrategyB implements DiscountStrategy {

        @Override
        public void execute() {
            System.out.println("执行优惠计价方式B 的复杂逻辑");
        }
    }

    public static class DiscountStrategyC implements DiscountStrategy {

        @Override
        public void execute() {
            System.out.println("执行优惠计价方式C 的复杂逻辑");
        }
    }

    public static class DiscountStrategyFactory {

        public static DiscountStrategy getDiscountStrategy(int discountStyle) {
            if (discountStyle == 1) {
                return new DiscountStrategyA();
            } else if (discountStyle == 2) {
                return new DiscountStrategyB();
            } else {
                return new DiscountStrategyC();
            }
        }

    }

    public static class Context {
        private DiscountStrategy discountStrategy;

        public Context(DiscountStrategy discountStrategy) {
            this.discountStrategy = discountStrategy;
        }

        public void execuete() {
            discountStrategy.execute();
        }
    }
}
